package 算法.排序;

public class 插入 {
    public static void main(String[] args) {
        int[] arr={16,5,9,12, 21,18, 32,23,37,26,45,34, 50,48,61,52,73,66};

        // 找到无序的那一组是从哪个索引开始的
        int stratIndex=-1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > arr[i+1]) {
                stratIndex=i+1 ;
                break;
            }
        }
        for (int i = stratIndex; i < arr.length; i++) {
            int j=i;
            while (j>0 && arr[j]<arr[j-1]){
                swap(arr,j,j-1);
                j--;
            }
        }
        printArr(arr);
    }
    private static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
    private static void swap(int[] arr, int a, int b) {
        int temp=arr[a];
        arr[a]=arr[b];
        arr[b]=temp;
    }

}
